package com.example.interfacecall.controller;

import com.alibaba.fastjson.JSON;
import com.example.interfacecall.domain.ClaimsNoticeDto;
import com.example.interfacecall.domain.GetBaoHanInfoInDto;
import com.example.interfacecall.domain.GetBaoHanInfoResult;
import com.example.interfacecall.domain.Result;
import com.example.interfacecall.exception.BusinessException;
import com.example.interfacecall.util.HttpUtils;
import com.example.interfacecall.util.ResultWrapper;
import com.example.interfacecall.util.ResultWrapperMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@Validated
@RestController
public class GetBaoHanInfoController {
    @Value("${interface.url}")
    private String getbaohaninfoUrl;

    @GetMapping("/getBaoHanInfoPage")
    public ModelAndView getBaoHanInfoPage() {
        return new ModelAndView("getBaoHanInfo");
    }

    @PostMapping("/getBaoHanInfo")
    public ResultWrapper<GetBaoHanInfoResult> getBaoHanInfo(@Valid @RequestBody @ModelAttribute(value = "getbaohaninfo") GetBaoHanInfoInDto getBaoHanInfoInDto) {
        try{
            String interfaceName = "getbaohaninfo";
            String url = getbaohaninfoUrl +"/"+ interfaceName;
            String param = JSON.toJSONString(getBaoHanInfoInDto);


            String resultOut = HttpUtils.sendPost(url, param);

            @Valid
            GetBaoHanInfoResult result = JSON.parseObject(resultOut, GetBaoHanInfoResult.class);
            return ResultWrapperMapper.ok(result);
        } catch (BusinessException e){
            return ResultWrapperMapper.wrap(e);
        }
    }
}
